import {statisticsApi, removeApi, getIdiomApi, subjectKnowledgeApi, singleQuestionApi, resultApi, singleApi} from '@/api/wrongTopicApi.js'

const wrongTopic = {
    namespaced: true,
    state: {
     // 用户选择的学科
     selectSubjectData:  localStorage.selectSubjectData ? JSON.parse(localStorage.selectSubjectData) : '',
     // 在主页用户点击的错题类型统计
     keyPointsData: localStorage.keyPointsData ? JSON.parse(localStorage.keyPointsData) : '',
     // 测试错题 0 & 联系错题 1
     wrongTopicTypeData: localStorage.wrongTopicTypeData ? JSON.parse(localStorage.wrongTopicTypeData) : 0,
     // 获取到举一反三的题目
     allSubjectArr: localStorage.allSubjectArr ? JSON.parse(localStorage.allSubjectArr) : '',
     // 举一反三答题结果统计
     allResultData: localStorage.allResultData ? JSON.parse(localStorage.allResultData) : '',
     // 单题答题结果
     singleData: localStorage.singleData ? JSON.parse(localStorage.singleData) : '',
     // 判断从哪个页面到答题页
     whereabouts: localStorage.whereabouts ? JSON.parse(localStorage.whereabouts) : '',
     // 左上角要提现的知识点
     reflectText: localStorage.reflectText ? JSON.parse(localStorage.reflectText) : ''
    },
    getters: {
    },
    mutations: {
      controlKeyPointsData (state, val) {
        state.keyPointsData = val
        localStorage.keyPointsData = JSON.stringify(val)
      },
      controlWrongTopicTypeData (state, val) {
        state.wrongTopicTypeData = val
        localStorage.wrongTopicTypeData = JSON.stringify(val)
      },
      controlSelectSubject (state, val) {
        state.selectSubjectData = val
        localStorage.selectSubjectData = JSON.stringify(val)
      },
      controlAllSubjectArr (state, val) {
        state.allSubjectArr = val
        localStorage.allSubjectArr = JSON.stringify(val)
      },
      controlAllResultData (state, val) {
        state.allResultData = val
        localStorage.allResultData = JSON.stringify(val)
      },
      controlSingleData (state, val) {
        state.singleData = val
        localStorage.singleData = JSON.stringify(val)
      },
      controlWhereabouts (state, val) {
        state.whereabouts = val
        localStorage.whereabouts = JSON.stringify(val)
      },
      controlReflectText (state, val) {
        state.reflectText = val
        localStorage.reflectText = JSON.stringify(val)
      },
    },
    actions: {
      // 错题统计 （ 环形图 以及 题目分类总数统计 ）
      statisticsApi ({state, commit, dispatch}, params) {
        return new Promise((resolve, reject) => {
          statisticsApi(params).then(res => {
              resolve(res)
            }).catch(err => {
              reject(err)
            })
        })
      },

      // 错题统计 （ 学科 以及 知识点数目统计 ）
      subjectKnowledgeApi ({state, commit, dispatch}, params) {
        return new Promise((resolve, reject) => {
          subjectKnowledgeApi(params).then(res => {
                resolve(res)
            })
        })
      },

      // 移除错题
      removeApi ({state, commit, dispatch}, params) {
        return new Promise((resolve, reject) => {
          removeApi(params).then(res => {
                resolve(res)
            })
        })
      },

      // 获取举一反三题目
      getIdiomApi ({state, commit, dispatch}, params) {
        return new Promise((resolve, reject) => {
          getIdiomApi(params).then(res => {
              resolve(res)
            }).catch(err => {
              reject(err)
            })
        })
      },

      // 提交单题用户答案  （举一反三点提交）
      singleQuestionApi ({state, commit, dispatch}, params) {
        return new Promise((resolve, reject) => {
          singleQuestionApi(params).then(res => {
              resolve(res)
            }).catch(err => {
              reject(err)
            })
        })
      },

      // 答题结果页  （举一反三点）
      resultApi ({state, commit, dispatch}, params) {
        return new Promise((resolve, reject) => {
          resultApi(params).then(res => {
              resolve(res)
            }).catch(err => {
              reject(err)
            })
        })
      },

      // 单个题目答题解析  （举一反三）
      singleApi ({state, commit, dispatch}, params) {
        return new Promise((resolve, reject) => {
          singleApi(params).then(res => {
              resolve(res)
            }).catch(err => {
              reject(err)
            })
        })
      }
    }
}
export default wrongTopic
